package net.ezsteam.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.kobjects.base64.Base64;
import org.ksoap2.HeaderProperty;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Toast;

public class EzSteam_Login_Service extends AsyncTask<String, Void, String> {

	private ProgressDialog progdialod;
	private Activity activity;
	private Context context;

	private String userName;
	private String password;

	// connection establish timeout (timeoutConnection)
	private int timeoutConnection = 5000;
	// socket timeout (timeoutSocket)
	private int timeoutSocket = 45000;

	private static byte[] buff = new byte[1024];
	private String retval = "";

	private ArrayAdapter<String> listAdapter;

	private int[] app_ids;

	JSONArray jArray;

	/*private static final String NAMESPACE = "urn:ezsteam_project";
	private static final String URL = "http://ezsteam.demoplease.net/webservice/request.php?wsdl";
	private static String SOAP_ACTION = "urn:ezsteam_project#runweb";
	private static final String METHOD_NAME = "runweb";*/
	
	private static String SOAP_ACTION = "urn:ezsteam_project#runweb";
	private static String NAMESPACE = "urn:ezsteam_project";
	private static String METHOD_NAME = "runweb";
	private static String URL = "http://ezsteam.demoplease.net/webservice/request.php?wsdl";

	private static final String TAG = null;

	public EzSteam_Login_Service(Activity activity, String user, String password) {
		super();
		this.activity = activity;
		this.context = this.activity.getApplicationContext();
		this.userName = user;
		this.password = password;
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		progdialod = ProgressDialog.show(this.activity, "User Login",
				"Logging In, Please Wait", true, false);
	}

	@Override
	protected String doInBackground(String... urls) {

		try {

			SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);

/*			Vector<String> mSkuList= new Vector<String>();

            mSkuList.add("s.ratheepan@gmail.com");
            mSkuList.add("ratheepan");
            */

			Request.addProperty("data", "{\"method\":\"userLogin\",\"data\":{\"record1\":\"s.ratheepan@gmail.com\",\"record2\":\"ratheepan\"}}");
			
			//Request.addProperty("method", "userLogin");
			//Request.addProperty("data","{\"record1\":\"s.ratheepan@gmail.com\",\"record2\":\"ratheepan\"}");
			/*Request.addProperty("record1","s.ratheepan@gmail.com");
			Request.addProperty("record2","ratheepan");*/

			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            
			Log.e("Request", Request.toString());
			
			envelope.bodyOut = Request;
			
			envelope.dotNet=true;
			
			//SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
			envelope.setOutputSoapObject(Request);
			envelope.dotNet= true;

			HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

			//call the webservice
			androidHttpTransport.call(SOAP_ACTION, envelope);

			String str = "";
			
			SoapObject result = (SoapObject) envelope.bodyIn;
			str = result != null ? result.getProperty(0).toString() : "0";

		return str;
		
		} catch (Exception e) {

			e.printStackTrace();
			return e.toString();
			}
	}

	@Override
	protected void onPostExecute(String result) {

		Log.d("Post Exec", "POST EXEC");

		Log.d("Post tt", result);
		progdialod.dismiss();
		
		try {
			
			JSONArray jarr = new JSONArray(result);

			if(jarr.length()==1){
			
				JSONObject jsonobj = jarr.getJSONObject(0);
				
				String login_state = jsonobj.getString("login_state");
				
				if(login_state.equals("success")){
					
					JSONObject json_customer = jsonobj.getJSONObject("customer");
					
					String id = json_customer.getString("id");
					String name = json_customer.getString("name");
					String email = json_customer.getString("email");
					String type = json_customer.getString("type");

					Toast toast = Toast.makeText(activity, "success. For user "+name, Toast.LENGTH_SHORT);
					toast.show();
				}
	
			}else{
				Toast toast = Toast.makeText(activity, "Login Failed !!", Toast.LENGTH_SHORT);
				toast.show();
			}
			
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
